﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIM.model;

namespace LIM.controller
{
    public class DanhMucKhachHangController : BaseController
    {
        public DanhMucKhachHangController()
        { 
        }
        public tbl_CRM_KhachHang LoadByID(int RowID)
        {
            return LIMS_Entities.tbl_CRM_KhachHang.Where(p => p.RowID == RowID).FirstOrDefault();
        }
        public object Load()
        {
            return LIMS_Entities.tbl_CRM_KhachHang.ToList().Select(p => new 
                                                            {
                                                                RowID = p.RowID,
                                                                RowIDParent = p.RowIDParent,
                                                                Ma = p.MaKhachHang,
                                                                TenGoi=p.TenGoi,
                                                                TenGoiNgan=p.TenGoiNgan,
                                                                DiaChi=p.DiaChi,
                                                                QuanHuyen = p.RowIDQuanHuyen != null ?  LIMS_Entities.tbl_DanhMuc_QuanHuyen.Where(o=>o.RowID == p.RowIDQuanHuyen).FirstOrDefault().TenGoi : "",
                                                                TinhThanh= p.RowIDTinhThanh != null ?  LIMS_Entities.tbl_DanhMuc_TinhThanh.Where(o=>o.RowID == p.RowIDTinhThanh).FirstOrDefault().TenGoi : "",
                                                                QuocGia = p.QuocGia,
                                                                DanhGia = p.DanhGiaKhachHang != null ? LIMS_Entities.tbl_DanhMuc_HeThong.Where(o => o.MaDanhMuc == p.DanhGiaKhachHang).FirstOrDefault().TenGoi : "",
                                                                TinhTrang = p.TinhTrangKhachHang != null ? LIMS_Entities.tbl_DanhMuc_HeThong.Where(o => o.MaDanhMuc == p.TinhTrangKhachHang).FirstOrDefault().TenGoi : "",
                                                                NhanVienQuanLy = p.NhanVienChamSocKhachHang,//p.NhanVienChamSocKhachHang != null ? LIMS_Entities.tbl_SYS_Users.Where(o => o.UserName == p.NhanVienChamSocKhachHang).FirstOrDefault().TenGoi : "",
                                                            }).ToList();
        }
        public bool IsCheckConstainRowIDKhachHang(int RowIDKhachHang)
        {
            bool Result = false;
            if (LIMS_Entities.tbl_DanhMuc_ChiTieu_BangGia.Where(p => p.RowIDKhachHang == RowIDKhachHang).ToList().Count > 0)
            { Result = true; }
            if (LIMS_Entities.tbl_CRM_NhatKyLienLac.Where(p => p.RowIDKhachHang == RowIDKhachHang).ToList().Count > 0)
            { Result = true; }
            if (LIMS_Entities.tbl_CRM_BaoGia.Where(p => p.RowIDKhachHang == RowIDKhachHang).ToList().Count > 0)
            { Result = true; }
            if (LIMS_Entities.tbl_CRM_HopDong.Where(p => p.RowIDKhachHang == RowIDKhachHang).ToList().Count > 0)
            { Result = true; }
            if (LIMS_Entities.tbl_CRM_PhieuNhanMau.Where(p => p.RowIDKhachHang == RowIDKhachHang).ToList().Count > 0)
            { Result = true; }
            if (LIMS_Entities.tbl_CRM_LienHeKhachHang.Where(p => p.RowIDKhachHang == RowIDKhachHang).ToList().Count > 0)
            { Result = true; }
            return Result;
        }
        public List<tbl_DanhMuc_QuanHuyen> LoadDMQuanHuyen(int? RowIDTinhThanh)
        {
            List<tbl_DanhMuc_QuanHuyen> lstQuanHuyen = new List<tbl_DanhMuc_QuanHuyen>();
            if (RowIDTinhThanh != null)
            {
                lstQuanHuyen = LIMS_Entities.tbl_DanhMuc_QuanHuyen.Where(p => p.RowIDTinhThanh == RowIDTinhThanh).ToList();
                tbl_DanhMuc_QuanHuyen quanhuyen = new tbl_DanhMuc_QuanHuyen();
                quanhuyen.TenGoi = "-------------- Chọn Quận huyện -------------";
                lstQuanHuyen.Insert(0, quanhuyen);
            }
            return lstQuanHuyen;
        }
        public List<tbl_DanhMuc_TinhThanh> LoadDMTinhThanh()
        {
            return LIMS_Entities.tbl_DanhMuc_TinhThanh.ToList();
        }
        public List<tbl_CRM_KhachHang> LoadLaCongtyConCua(int? IDKhachHang)
        {
            List<tbl_CRM_KhachHang> lstKHachHang = new List<tbl_CRM_KhachHang>();
            if (IDKhachHang != null)
            {
                lstKHachHang =  LIMS_Entities.tbl_CRM_KhachHang.Where(p => p.RowID != IDKhachHang).ToList();
            }
            else
            {
                lstKHachHang =  LIMS_Entities.tbl_CRM_KhachHang.ToList();
            }
            return lstKHachHang;
        }
        public List<tbl_DanhMuc_HeThong> LoadDMHeThong(int RowParentID)
        {
            return LIMS_Entities.tbl_DanhMuc_HeThong.Where(p=>p.RowIDParent == RowParentID).ToList();
        }
        public List<tbl_DanhMuc_ThongTinNhanVien> LoadDMThongTinNhanVien()
        {
            return LIMS_Entities.tbl_DanhMuc_ThongTinNhanVien.ToList();
        }

        public List<KhachHangInfo> LoadKhanhHang_PhieuNhanMau()
        {
            return LIMS_Entities.tbl_CRM_KhachHang.Select(p => new KhachHangInfo
            {
                RowID = p.RowID,
                MaKhachHang = p.MaKhachHang,
                TenGoi = p.TenGoi,
                TenGoiNgan = p.TenGoiNgan,
                DiaChi = p.DiaChi + (p.RowIDQuanHuyen != null ? ", " + LIMS_Entities.tbl_DanhMuc_QuanHuyen.Where(o => o.RowID == p.RowIDQuanHuyen).FirstOrDefault().TenGoi : "") + (p.RowIDTinhThanh != null ? ", " + LIMS_Entities.tbl_DanhMuc_TinhThanh.Where(o => o.RowID == p.RowIDTinhThanh).FirstOrDefault().TenGoi : ""),
                SoDienThoai = p.SoDienThoai,
                SoFax = p.SoFax,
                MaSoThue = p.MaSoThue,
                TaiKhoanNganHang = p.TaiKhoanNganHang,
                NhanVienQuanLy = p.NhanVienChamSocKhachHang
            }).ToList();
        }

        public KhachHangInfo LoadKhanhHang_PhieuNhanMau(int rowID)
        {
            return LIMS_Entities.tbl_CRM_KhachHang.Where(w => w.RowID == rowID).Select(p => new KhachHangInfo
            {
                RowID = p.RowID,
                MaKhachHang = p.MaKhachHang,
                TenGoi = p.TenGoi,
                TenGoiNgan = p.TenGoiNgan,
                DiaChi = p.DiaChi + (p.RowIDQuanHuyen != null ? ", " + LIMS_Entities.tbl_DanhMuc_QuanHuyen.Where(o => o.RowID == p.RowIDQuanHuyen).FirstOrDefault().TenGoi : "") + (p.RowIDTinhThanh != null ? ", " + LIMS_Entities.tbl_DanhMuc_TinhThanh.Where(o => o.RowID == p.RowIDTinhThanh).FirstOrDefault().TenGoi : ""),
                SoDienThoai = p.SoDienThoai,
                SoFax = p.SoFax,
                MaSoThue = p.MaSoThue,
                TaiKhoanNganHang = p.TaiKhoanNganHang,
                NhanVienQuanLy = p.NhanVienChamSocKhachHang
            }).FirstOrDefault();            
        }
    }
}
